package org.enhance.common.util;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.enhance.common.exception.EnhanceRuntimeException;

/**
 * @author JiangGengchao
 * @classname XmlUtil
 * @date 2021-11-26
 **/
public class XmlUtil {

	private XmlUtil() {
		throw new IllegalStateException("Constant class");
	}

	public static Map<String, Object> parseXml(String xmlStr) {
		Map<String, Object> result = new HashMap<>();
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

			Iterator<Element> iterator = document.getRootElement().elementIterator();
			while (iterator.hasNext()) {
				Element element = iterator.next();
				result.put(element.getName(), element.getTextTrim());
			}
		} catch (Exception e) {
			throw new EnhanceRuntimeException(e);
		}
		return result;
	}
}
